package backup;

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import cs6238.common.Configuration;

public class TestServer {

	public static void main(String[] args){
		
		try{
			Configuration serverConfig = Configuration.loadServerSettings();
			int serverPort = Integer.parseInt(serverConfig.getSetting(Configuration.NETWORK_PORT));
			
			String keyStorePath = serverConfig.getSetting(Configuration.KEYSTORE_PATH);
			String keyStorePassword = serverConfig.getSetting(Configuration.KEYSTORE_PASSWORD);
			String trustStorePath = serverConfig.getSetting(Configuration.TRUSTSTORE_PATH);
			String trustStorePassword = serverConfig.getSetting(Configuration.TRUSTSTORE_PASSWORD);
			
			System.setProperty("javax.net.debug", "all");
			System.setProperty("javax.net.ssl.keyStore", keyStorePath);
			System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
			System.setProperty("javax.net.ssl.trustStore", trustStorePath);
			System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);

			SSLServerSocketFactory serverSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
			SSLServerSocket serverSocket = (SSLServerSocket) serverSocketFactory.createServerSocket(serverPort);
			serverSocket.setNeedClientAuth(true);

			SSLSocket clientSocket = (SSLSocket)serverSocket.accept();
			
			byte[] fromClient = new byte[10];
			clientSocket.getInputStream().read(fromClient);
			
			for(int i=0; i<fromClient.length; i++){
				System.out.println(fromClient[i]);
			}
			
			clientSocket.close();
			serverSocket.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		System.out.println("All done");
	}
}
